<?php
    namespace app\common\job;

    use think\queue\Job;
    use app\common\model\Storage;
    use app\common\job\AbstractJob;
    use think\db\exception\DbException;



    class SynchronizationStorage extends AbstractJob {
        /**
         * @param Job $job
         * @param $data
         */
        public function fire(job $job,$data){
            try {
                $tid = isset($data['tid'])?$data['tid']:'';
                $ids = isset($data['ids'])?$data['ids']:'';
                $storage_info_model   = new Storage();
                if($tid){
                    $list                 = $storage_info_model->getDeletedListByTid($tid);
                }else{
                    $where['oi_id']     = ['in',$ids] ;
                    $list                 = $storage_info_model->getDeletedList($where);
                }
                $total                = 0;

                foreach ($list as $val){
                    $update_info = json_decode($val['oi_update_storage'],true);
                   if($update_info){
                       foreach ($update_info as $k=>$v){
                           $row = $storage_info_model->find($k);
                           if($row){
                               $row->oi_year = $val['oi_year'];
                               $row->oi_unit = $val['oi_unit'];
                               $row->oi_cate_id = $val['oi_cate_id'];
                               $row->oi_cate_name = $val['oi_cate_name'];
                               $row->oi_specs = $val['oi_specs'];
                               $row->oi_bottle_num = $val['oi_bottle_num'];
                               $row->oi_date = $val['oi_date'];
                               $row->oi_batch = $val['oi_batch'];
                               $row->oi_code = $val['oi_code'];
                               $row->oi_bottle_code = $val['oi_bottle_code'];
                               $row->oi_is_fear_query = $val['oi_is_fear_query'];
                               //修改的出入库信息如果是入库  需要修改出库的成本减
                               if($val['oi_warehouse'] == 2){
                                   $row->oi_purchase_price = $val['oi_price'];
                               }
                               //修改的出入库信息如果是出库  需要修改入库的价格
                               if($val['oi_warehouse'] == 1){
                                   $row->oi_price = $val['oi_purchase_price'];
                               }

                               $row->save();
                               $total++;
                           }
                       }
                   }

                }
                $job->delete();
                return true;
            }catch (DbException $e) {
                $job->delete();
                return false;
            }


        }
    }